﻿$(document).ready(function() {
    $("input.checkbox-fraud").change(function () {
        var value = $(this).val();
        var str = $("#listorder").val();
        if (this.checked) {
            str = str + value + ",";
            $("#listorder").val(str);
        } else {
            str = str.replace(value + ",", "");
            $("#listorder").val(str);
        }
    });

});

function loadFraudTable() {
    App.blockUI($(".checker-tbl"));
    $.ajax({
        url: '/Sale/LoadFraudTable',
        type: "POST",
        success:function(data) {
            $(".checker-tbl").html(data);
            InitDatatable();
            InitTableSelected();
            App.unblockUI($(".checker-tbl"));
        },
        error:function() {
            App.unblockUI($(".checker-tbl"));
        }
    });
}
function orderDetail(orderref) {
    App.blockUI($('.order-detail'), false);
    $.ajax({
        url: '/Sale/OrderDetails',
        type:"POST",
        data: { orderref: orderref },
        success: function (data) {
            if (data.success == 1) {
                $("#transactionid").val(data.dataObject.TransactionID);
                $("#orderref").val(data.dataObject.Orderref);
                if (data.dataObject.FirstName != null && data.dataObject.Surname != null) {
                    $("#customername").val(data.dataObject.FirstName + " " + data.dataObject.Surname);
                }
                $("#fraudnote").val(data.dataObject.notes);
                $("#message").val(data.dataObject.Message);
                $("#amount").val("£" + data.dataObject.Amount);

                var shipaddress = "", billaddress = "";
                if (data.dataObject.BillAdd1.length > 0) {
                    billaddress += data.dataObject.BillAdd1 + "\r\n";
                }
                if (data.dataObject.BillAdd2.length > 0) {
                    billaddress += data.dataObject.BillAdd2 + "\r\n";
                }
                if (data.dataObject.BillCity.length > 0) {
                    billaddress += data.dataObject.BillCity + "\r\n";
                }
                if (data.dataObject.BillCounty.length > 0) {
                    billaddress += data.dataObject.BillCounty + "\r\n";
                }
                if (data.dataObject.BillCountry.length > 0) {
                    billaddress += data.dataObject.BillCountry + "\r\n";
                }
                if (data.dataObject.BillPostcode.length > 0) {
                    billaddress += data.dataObject.BillPostcode;
                }

                if (data.dataObject.ShipAdd1.length > 0) {
                    shipaddress += data.dataObject.ShipAdd1 + "\r\n";
                }
                if (data.dataObject.ShipAdd2.length > 0) {
                    shipaddress += data.dataObject.ShipAdd2 + "\r\n";
                }
                if (data.dataObject.ShipCity.length > 0) {
                    shipaddress += data.dataObject.ShipCity + "\r\n";
                }
                if (data.dataObject.ShipCounty.length > 0) {
                    shipaddress += data.dataObject.ShipCounty + "\r\n";
                }
                if (data.dataObject.ShipCountry.length > 0) {
                    shipaddress += data.dataObject.ShipCountry + "\r\n";
                }
                if (data.dataObject.ShipPostcode.length > 0) {
                    shipaddress += data.dataObject.ShipPostcode;
                }

                $("#shippingaddress").val(shipaddress);
                $("#billingaddress").val(billaddress);
                App.unblockUI($('.order-detail'));
            }
        }
    });
}
function saveNotes() {
    var tranid = $("#transactionid").val();
    var notes = $("#fraudnote").val();
    if (tranid != null) {
        App.blockUI($('body'));
        $.ajax({
            url: '/Sale/SaveNotes',
            type: "POST",
            data: { transactionid: tranid, notes: notes },
            success: function(data) {
                if (data.success == 1) {
                    $("#transactionid").val("");
                    $("#orderref").val("");
                    $("#customername").val("");
                    $("#fraudnote").val("");
                    $("#message").val("");
                    $("#amount").val("");
                    $("#shippingaddress").val("");
                    $("#billingaddress").val("");
                    $(".tbl-fraudchecked tbody tr").removeClass("row-selected");
                } else {
                    alert("The specified TransactionID is not valid.");
                }
                App.unblockUI($('body'));
            },
            error: function() {
                alert("error");
                App.unblockUI($('body'));
            }
        });
    } else {
        alert("Please choose an order !!");
    }
}
function approveOrder() {
    var tranid = $("#transactionid").val();
    var orderref = $("#orderref").val();
    var notes = $("#fraudnote").val();
    if (tranid != null) {
        if (confirm("Are you sure you wish to approve this order?")) {
            App.blockUI($('body'));
            $.ajax({
                url: '/Sale/ApproveOrder',
                type: "POST",
                data: { transactionid: tranid, orderref:orderref },
                success: function(data) {
                    if (data.success == 1) {
                        
                        $.ajax({
                            url: '/Sale/SaveNotes',
                            type: "POST",
                            data: { transactionid: tranid, notes: notes }
                        });
                        
                        $("#transactionid").val("");
                        $("#orderref").val("");
                        $("#customername").val("");
                        $("#fraudnote").val("");
                        $("#message").val("");
                        $("#amount").val("");
                        $("#shippingaddress").val("");
                        $("#billingaddress").val("");
                        $(".tbl-fraudchecked tbody tr").removeClass("row-selected");
                        alert("OrderRef: " + data.orderref + " Approved");
                        loadFraudTable();
                    } else {
                        alert("The specified TransactionID is not valid.");
                    }
                    App.unblockUI($('body'));
                },
                error: function() {
                    alert("error");
                    App.unblockUI($('body'));
                }
            });
        }
    } else {
        alert("Please choose an order to approve !!");
    }
}

function approveListOrder() {
    var lsttranid = $("#listorder").val();
    if (lsttranid != null) {
        if (confirm("Are you sure you wish to approve the selected orders?")) {
            App.blockUI($('body'));
            $.ajax({
                url: '/Sale/ApproveListOrder',
                type: "POST",
                data: { listorder: lsttranid },
                success: function (data) {
                    if (data.success == 0) {
                        alert("Error !!");
                    } else {
                        $("#transactionid").val("");
                        $("#orderref").val("");
                        $("#customername").val("");
                        $("#fraudnote").val("");
                        $("#message").val("");
                        $("#amount").val("");
                        $("#shippingaddress").val("");
                        $("#billingaddress").val("");
                        alert("OrderRef: " + data.listorderref + " Approved");
                        loadFraudTable();
                    }
                    App.unblockUI($('body'));
                },
                error: function () {
                    alert("Error !!");
                    App.unblockUI($('body'));
                }
            });
        }
    } else {
        alert("Please check one or more orders to approve !!");
    }
}